package com.rs.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.rs.entities.Hotel;
import com.rs.entities.Nearby;
import com.rs.entities.Restaurant;
import com.rs.entities.Scenic;
import com.rs.entities.ScenicRequest;
import com.rs.utils.DistanceUtil;

public class TouristInfoDao extends BaseDao {

	@SuppressWarnings("unchecked")
	public List<Scenic> getScenicList(ScenicRequest request) {
		Criteria criteria = createScenicCriteria(request);
		List<Scenic> scenicList = criteria.list();
		System.out.println(scenicList.size());
		List<Scenic> result;
		if (request != null && (request.getCode() == null || request.getCode() == 1) && request.getDistanceStart() >= 0
				&& request.getDistanceEnd() >= 0 && request.getDistanceEnd() > request.getDistanceStart()) {
			result = new ArrayList<>();
			for (int i = 0; i < scenicList.size(); i++) {
				double distance = DistanceUtil.GetShortDistance(request.getLongitude(), request.getLatitude(),
						scenicList.get(i).getLongitude(), scenicList.get(i).getLatitude());
				if (distance >= request.getDistanceStart() && distance <= request.getDistanceEnd()) {
					result.add(scenicList.get(i));
				}
			}
		} else {
			result = scenicList;
		}
		return result;
	}

	private Criteria createScenicCriteria(ScenicRequest request) {
		Criteria criteria = super.getSession().createCriteria(Scenic.class);
		criteria.setMaxResults(100);
		if (request == null || (request.getCode() != null && request.getCode() == 1)) {
			return criteria;
		} else if (request.getCode() != null && request.getCode() == 2) {
			System.out.println("get one");
			criteria.add(Restrictions.eq("longitude", request.getLongitude()));
			criteria.add(Restrictions.eq("latitude", request.getLatitude()));
			return criteria;
		} else if (request.getCode() != null && request.getCode() == 3) {
			System.out.println("search");
			criteria.add(Restrictions.or(Restrictions.like("city", "%" + request.getCity() + "%"),
					Restrictions.like("province", "%" + request.getProvince() + "%"),
					Restrictions.like("district", "%" + request.getDistrict() + "%"),
					Restrictions.like("name", "%" + request.getType() + "%"),
					Restrictions.like("type", "%" + request.getType() + "%")));
			return criteria;
		}

		if (request.getProvince() != null && !request.getProvince().equals("all")) {
			criteria.add(Restrictions.eq("province", request.getProvince()));
		}
		if (request.getCity() != null && !request.getCity().equals("all")) {

			criteria.add(Restrictions.eq("city", request.getCity()));
		}
		if (request.getDistrict() != null && !request.getDistrict().equals("all")) {
			criteria.add(Restrictions.eq("district", request.getDistrict()));
		}
		if (request.getType() != null && !request.getType().equals("all")) {
			String[] types = request.getType().split(",");
			for (int i = 0; i < types.length; i++) {
				criteria.add(Restrictions.like("type", types[i]));
			}
		}
		if (request.getPriceStart() >= 0 && request.getPriceEnd() >= 0
				&& request.getPriceEnd() >= request.getPriceStart()) {
			criteria.add(Restrictions.between("price", request.getPriceStart(), request.getPriceEnd()));
		}
		return criteria;
	}

	public List<Hotel> getHotelList(Nearby nearby) {
		Criteria criteria = createHotelCriteria(nearby);
		List<Hotel> hotelList = criteria.list();
		List<Hotel> result;
		/*
		 * if (nearby != null) { result = new ArrayList<>(); for (int i = 0; i <
		 * hotelList.size(); i++) { double distance =
		 * DistanceUtil.GetShortDistance(nearby.getLongitude(),
		 * nearby.getLatitude(), hotelList.get(i).getLongitude(),
		 * hotelList.get(i).getLatitude()); if (distance >=
		 * nearby.getDistanceStart() && distance <= nearby.getDistanceEnd()) {
		 * result.add(hotelList.get(i)); } } } else { result = hotelList; }
		 */
		result = hotelList;
		return result;
	}

	private Criteria createHotelCriteria(Nearby nearby) {
		Criteria criteria = super.getSession().createCriteria(Hotel.class);
		return criteria;
	}

	public List<Restaurant> getRestaurantList(Nearby nearby) {
		Criteria criteria = createRestaurantCriteria(nearby);
		List<Restaurant> scenicList = criteria.list();
		System.out.println(scenicList.size());
		List<Restaurant> result;
		/*
		 * if (nearby != null) { for (int i = 0; i < scenicList.size(); i++) {
		 * double distance =
		 * DistanceUtil.GetShortDistance(nearby.getLongitude(),
		 * nearby.getLatitude(), scenicList.get(i).getLongitude(),
		 * scenicList.get(i).getLatitude()); System.out.println("Distance:" +
		 * distance); if (distance >= nearby.getDistanceStart() && distance <=
		 * nearby.getDistanceEnd()) { result.add(scenicList.get(i)); } } } else
		 * { result = scenicList; }
		 */
		result = scenicList;
		return result;
	}

	private Criteria createRestaurantCriteria(Nearby nearby) {
		Criteria criteria = super.getSession().createCriteria(Restaurant.class);
		return criteria;
	}

	public void insert(String fileName) {
		/*
		 * String content = ReadFile.readFileByLines("D:/temp/" + fileName);
		 * String[] contents = content.split("\n"); if
		 * (fileName.contains("scenic")) { writeScenicToSql(contents); } else if
		 * (fileName.contains("hotel")) { writeHotelToSql(contents); } else if
		 * (fileName.contains("restaurant")) { writeRestaurnatToSql(contents); }
		 */
	}
	/*
	 * private void writeScenicToSql(String[] contents) { for (int i = 1; i <
	 * contents.length; i++) { String[] fields = contents[i].split(" "); Scenic
	 * scenic = new Scenic(); scenic.setName(fields[0]);
	 * scenic.setProvince(fields[1]); scenic.setCity(fields[2]);
	 * scenic.setDistrict(fields[3]);
	 * scenic.setLongitude(Double.parseDouble(fields[4]));
	 * scenic.setLatitude(Double.parseDouble(fields[5]));
	 * scenic.setPrice(Float.parseFloat(fields[6])); scenic.setType(fields[7]);
	 * scenic.setScore(Float.parseFloat(fields[8])); scenic.setExtra(fields[9]);
	 * getSession().save(scenic); } }
	 * 
	 * private void writeHotelToSql(String[] contents) { for (int i = 1; i <
	 * contents.length; i++) { String[] fields = contents[i].split(" "); Hotel
	 * hotel = new Hotel(); hotel.setName(fields[0]);
	 * hotel.setAddress(fields[1]); hotel.setTelephone(fields[2]);
	 * hotel.setLongitude(Double.parseDouble(fields[3]));
	 * hotel.setLatitude(Double.parseDouble(fields[4]));
	 * hotel.setPrice(Float.parseFloat(fields[5])); getSession().save(hotel); }
	 * }
	 * 
	 * private void writeRestaurnatToSql(String[] contents) { for (int i = 1; i
	 * < contents.length; i++) { String[] fields = contents[i].split(" ");
	 * Restaurant restaurant = new Restaurant(); restaurant.setName(fields[0]);
	 * restaurant.setAddress(fields[1]); restaurant.setTelephone(fields[2]);
	 * restaurant.setLongitude(Double.parseDouble(fields[3]));
	 * restaurant.setLatitude(Double.parseDouble(fields[4]));
	 * restaurant.setType(fields[5]); getSession().save(restaurant); } }
	 */
}
